package models

import (
	"golanger.com/utils"
	"jk/jklog"
	"labix.org/v2/mgo"
	"time"
)

type Firmware struct {
	Name       string
	CreateTime int64
	UpdateTime int64
	Classify   string
	Mode       string
	Content    string
	Fixed      bool
	StopTime   int64
	Operate    string
	StartTime  int64
}

var (
	// @FUNC read database from firmwaretest
	ColFirmware = utils.M{
		"name": "firmware",
	}
)

func GetCFirmware() *mgo.Collection {
	return GetC("db", ColFirmware)
}

func NewFirmware(name string, classify string, content string, mode string) *Firmware {
	return &Firmware{
		Name:       name,
		CreateTime: time.Now().Unix(),
		StartTime:  time.Now().Unix(),
		StopTime:   time.Now().Unix(),
		UpdateTime: time.Now().Unix(),
		Classify:   classify,
		Mode:       mode,
		Content:    content,
	}
}

func GetFirmwareAllItems(query interface{}, m interface{}) error {
	if err := GetCFirmware().Find(query).Sort("-createtime").All(m); err != nil {
		jklog.L().Errorln("Get Items Error")
		return err
	}
	return nil
}

/**
 * @FUNC get item
 * @param query = utils.M{ "classify": "xxx" }
 */
func GetFirmwareItem(query interface{}, save interface{}) error {
	return GetCFirmware().Find(query).One(save)
}

/**
 * @FUNC add to db
 */
func SaveItemToDB(item interface{}) error {
	return GetCFirmware().Insert(item)
}

func ModItemToDB(query interface{}, update interface{}) error {
	change := utils.M{
		"$set": update,
	}
	return GetCFirmware().Update(query, change)
}

func DelItemFromDB(query interface{}) error {
	return GetCFirmware().Remove(query)
}
